package com.bitbank.smartdevice.ui.home

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import com.bitbank.newarch.ui.mine.MineFragment
import com.bitbank.smartdevice.R
import com.bitbank.smartdevice.adapter.base.CommonAdapter
import com.bitbank.smartdevice.adapter.base.MViewHolder
import com.bitbank.smartdevice.adapter.base.SpacesItemDecoration
import com.bitbank.smartdevice.ui.base.BaseListFragment
import com.bitbank.smartdevice.vo.BannerItem
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import kotlinx.android.synthetic.main.fragment_home.view.*


/**
 *  Date：2018/12/10
 *  Author：xulee
 *  Desc：首页
 */

class HomeFramgent : BaseListFragment<BannerItem>() {

    override fun getLayoutID(): Int = R.layout.fragment_home

    override fun getRecycleView(v: View): RecyclerView {
        v.recycleView.layoutManager = LinearLayoutManager(context)
        v.recycleView.addItemDecoration(
            SpacesItemDecoration(
                QMUIDisplayHelper.dpToPx(10),
                QMUIDisplayHelper.dpToPx(16)
            )
        )
        return v.recycleView
    }

    override fun initView(v: View) {
        super.initView(v)
        v.topBar.setTitle("Marry's home")
        v.topBar.addRightImageButton(R.drawable.ic_menu_add_24dp, R.id.topbar_right_add_button).setOnClickListener {
            startFragment(ECFragment())
        }
    }

    override fun getInnerAdapter(): CommonAdapter<BannerItem> {
        val example0 = BannerItem(
            "https://img14.360buyimg.com/n1/s450x450_jfs/t1/1533/6/5237/158730/5b9f3eceE29188f86/f408c95922f1a145.jpg",
            "智能排座USL1.0.1 升级内测1"
        )
        val example1 = BannerItem(
            "https://img14.360buyimg.com/n4/jfs/t23545/103/2622406620/58635/5b687/5b889f1aNb490c6fb.jpg",
            "智能排座USL1.0.1 升级内测2"
        )
        var mDatas = arrayOf(example0, example1)
        return object : CommonAdapter<BannerItem>(context!!, R.layout.list_item_home, mDatas) {
            override fun convert(holder: MViewHolder, t: BannerItem, position: Int) {
                holder.setText(R.id.tvTitle, t.title).setImageUrl(R.id.ivBanner, t.img)
            }
        }
    }

    override fun loadData() {

    }

    override fun getHeaderView(): View? {
        return LayoutInflater.from(context).inflate(R.layout.layout_header_home, null, false)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        return when (item.itemId) {

            R.id.action_search -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}
